package com.yan.activity;
import java.net.URL;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;

import android.content.res.XmlResourceParser;
import android.util.Log;
import android.util.Xml;

public class XmlFeedParser {
   
    //Feed Parsing Method
    public ArrayList<Implement> parse(String url) {
       
        //Array of Episode Objects
        ArrayList<Implement> episodes = null;
        
        try {
            //Encode the URL into a URL Object
            URL episode_feed_url = new URL(url);
           
            //Open a Connection to the feed
            XmlPullParser parser = Xml.newPullParser();
            try {
                parser.setInput(episode_feed_url.openConnection().getInputStream(), null);
            } finally {
               
            }

            int event_type = parser.getEventType();
            Implement current_episode = null;
            boolean done = false;
            
            //Parse the feed, start reading throught the feed from top to bottom
            while (event_type != XmlResourceParser.END_DOCUMENT && !done){
               
                String tag_name = null;
                switch (event_type){
                    //Found the start of the feed
                    case XmlResourceParser.START_DOCUMENT:
                        episodes = new ArrayList<Implement>();
                        break;
                    //Found a start tag
                    case XmlResourceParser.START_TAG:
                       
                        //apply the data to our Episode object based on the tag name
                        tag_name = parser.getName();
                          Log.d("*********Current*********Tag*************", tag_name.toString());
                        if (tag_name.equalsIgnoreCase("item")){
                            current_episode = new Implement();
                        } else if (current_episode != null){
                            if (tag_name.equals("title")){
                                current_episode.setTitle(parser.nextText());
                                //Log.d("***********************DEBUG**************************",tag_name.toString( ));
                            } else if (tag_name.equals("pubDate")){
                                current_episode.setDate(parser.nextText());
                                //Log.d("***********************DEBUG**************************",tag_name.toString( ));
                            } else if (tag_name.equals("link")){
                                current_episode.setLink(parser.nextText());
                                //Log.d("***********************DEBUG**************************",tag_name.toString( ));
                            } else if (tag_name.equals("description")){
                                current_episode.setDescription(parser.nextText());
                                Log.d("*******************DESC*****************************",tag_name.toString( ));
                            }
                            
                        }
                        break;
                    //An end tag has been reached
                    case XmlResourceParser.END_TAG:
                        tag_name = parser.getName();
                        //End of an Episode Item
                        if (tag_name.equalsIgnoreCase("item") && current_episode != null){
                            episodes.add(current_episode);
                        //Reached the end of all episodes, no more data to collect
                        } else if (tag_name.equalsIgnoreCase("channel")){
                            done = true;
                        }
                        break;
                }
                event_type = parser.next();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        
        //Return the Episode Array
        return episodes;
    }

}